package cn.iocoder.yudao.module.resource.service.timepackage;

import cn.hutool.core.util.IdUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.resource.api.enums.EnumAipError;
import cn.iocoder.yudao.module.resource.controller.admin.timepackage.vo.TimePackagePageReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.timepackage.vo.TimePackageSaveReqVO;
import cn.iocoder.yudao.module.resource.dal.dataobject.timepackage.TimePackageDO;
import cn.iocoder.yudao.module.resource.dal.mysql.timepackage.TimePackageMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 时长套餐 Service 实现类
 *
 * @author lizhongwen
 */
@Service
@Validated
public class TimePackageServiceImpl implements TimePackageService {

    @Resource
    private TimePackageMapper timePackageMapper;

    @Override
    public String createTimePackage(TimePackageSaveReqVO createReqVO) {
        // 插入
        createReqVO.setId(IdUtil.objectId());
        TimePackageDO timePackage = BeanUtils.toBean(createReqVO, TimePackageDO.class);
        timePackageMapper.insert(timePackage);
        // 返回
        return timePackage.getId();
    }

    @Override
    public void updateTimePackage(TimePackageSaveReqVO updateReqVO) {
        // 校验存在
        validateTimePackageExists(updateReqVO.getId());
        // 更新
        TimePackageDO updateObj = BeanUtils.toBean(updateReqVO, TimePackageDO.class);
        timePackageMapper.updateById(updateObj);
    }

    @Override
    public void deleteTimePackage(String id) {
        // 校验存在
        validateTimePackageExists(id);
        // 删除
        timePackageMapper.deleteById(id);
    }

    private void validateTimePackageExists(String id) {
        if (timePackageMapper.selectById(id) == null) {
            throw exception(EnumAipError.TIME_PACKAGE_NOT_EXISTS);
        }
    }

    @Override
    public TimePackageDO getTimePackage(String id) {
        return timePackageMapper.selectById(id);
    }

    @Override
    public PageResult<TimePackageDO> getTimePackagePage(TimePackagePageReqVO pageReqVO) {
        return timePackageMapper.selectPage(pageReqVO);
    }

    @Override
    public List<TimePackageDO> getTimePackageList(Collection<String> ids) {
        return timePackageMapper.selectList(TimePackageDO::getId, ids);
    }

}